// pages/book/book.js
// import Dialog from '../../../miniprogram_npm/vant-weapp/dialog/dialog';
var common = require("../../../public");
import https from "../../../utils/http.js"
Page({
    data: {
        topTitle: "场地预订",
        active: 0,
        status: '0',
        orde: '',//订单编号
        paynowColor: "#fae16b",//立即支付初始背景色
        userInfo: {},//用户信息
        allPayment: [],//全部
        waitPayment: [],//代付款
        waitUse: [],//待付款
        completePayment: [],//完成付款
        TransactionClose: [],//交易关闭
        pageNum: 1,//第几页
        pageSize: 10,//每页显示的个数
        refreshing: false,
        isLoadingBusy: false

    },
    initData(number) {
        const that = this
        let tempJson = {
            userId: that.data.userInfo.user.id,
            token: that.data.userInfo.token,
            orderNo: number,
        }
        this.setData({
            isLoading: true
        })
        common.post('/venueOrder/getMyOrderInfo', tempJson, function (res) {
            if (res.status === 200) {
        
                if (res.data.orderInfo.refundStatus == 70) {  //退款中
                       common.showToast('正在申请退款中...')
                       
                }else if(res.data.orderInfo.refundStatus == 73){
                    that.reloadData() 
                }else{
                    wx.navigateTo({
                        url: '../ticket/ticket?orderno=' +  number + '&venuTrue=true',
                    })
                }
             
               

            } else {
                common.showRequestErrMsg(res)
            }
        }, function () {
            that.setData({
                isLoading: false
            })
        })
    },
    onShow() {
        var that = this
        let userInfo = wx.getStorageSync('user')
        this.setData({
            userInfo
        }, () => {
            that.reloadData()
        })
    },
    // 重新加载
    reloadData() {
        this.setData({
            allPayment: []
        }, () => {
            this.getAllPayment(true)  //第一加载传true
        })
    },
    // 全部订单
    async getAllPayment(refresh = false) {
        const that = this
        // 当前正在加载或没有获取到用户信息
        if (this.data.isLoadingBusy || !this.data.userInfo) {
            return
        }
        //是否在切换tab页
        if (refresh) {
            this.setData({
                noMoreData: false,
                refreshing: true,
                isLoadingBusy: true
            })
        } else {
            if (this.data.noMoreData == true) {
                return
            }
            this.setData({
                isLoadingBusy: true,
                refreshing: false
            })
        }
        // refresh为真代表了首次进入的全部或者是切换tabbaer
        const pageNum = refresh ? 1 : this.data.pageNum + 1
        var tempJson = {
            userId: this.data.userInfo.user.id,
            token: this.data.userInfo.token,
            status: that.data.status == 0 ? '' : that.data.status,  //全部
            pageNum: pageNum,
            pageSize: this.data.pageSize
        }
        let option = {
            url: "/venueOrder/getMyVenueOrderList",
            data: tempJson
        }
        let res = await https(option)
        if (res.data.status == 200) {
            // 更新页码
            that.setData({
                pageNum,
                allPayment: refresh ? res.data.data.result_list : [...that.data.allPayment, ...res.data.data.result_list]
            })
            if (res.data.data.result_list == null || res.data.data.result_list.length == 0) {
                that.setData({
                    noMoreData: true  //没有更多数据了
                })
            }
        } else {
            common.showRequestErrMsg(res)
        }
        that.setData({
            isLoadingBusy: false
        })
        if (refresh) {
            that.setData({
                refreshing: false
            })
        }
    },
    onChange(e) {
        this.data.status = e.detail.name
        this.reloadData()  //切换
    },
    onReachBottom() {
        this.getAllPayment(false)
    },

    // 跳转到确定支付页面
    toConfirm(e) {

        let { id, payment } = e.currentTarget.dataset.item
        var that = this
        var tempJson = {
            orderId: id,
            token: this.data.userInfo.token,
            openid: this.data.userInfo.openid,
            userId: this.data.userInfo.user.id
        };
        common.wxPayRequest("/chinaumsPayForVenue", tempJson, function (res) {
            // 测试接口暂时不能吊起
            if (res.msg == 'OK') {
                wx.requestPayment({
                    'timeStamp': res.data.timeStamp,
                    'nonceStr': res.data.nonceStr,
                    'package': res.data.package,
                    'signType': res.data.signType,
                    'paySign': res.data.paySign,
                    'success': function (res) {
                        common.showToast('支付成功', 'success')
                        that.reloadData()
                        that.setData({
                            showPop: false,
                            backToApp: true
                        }, _ => {
                            //   // that.getDrawInfo()
                            //   wx.navigateTo({
                            //     url: '../payment/payment?orderNo='+orderNo+'&money='+payment
                            // })
                        })
                    },
                    'fail': function (res) {
                        if (res.errMsg === 'requestPayment:fail cancel') {
                        } else {
                            common.showToast(res.errMsg)
                        }

                    }
                })
            }
        })
    },
    // 跳转详情
    toOrderDetail(e) {
        let {
            item
        } = e.currentTarget.dataset
        console.log(item)
        var url
        if (item.status == 0 || item.status == 1 || item.status == 6) {
            url = '/packageA/pages/Goods/book/bookDetail/bookDetail?orderNo=' + item.orderNo
        } else if (item.status == 2 || item.status == 3) {
            url = '/packageA/pages/Goods/book/refund/refund?orderNo=' + item.orderNo
        } else if (item.status == 7) {
            url = '/packageA/pages/Goods/book/refundDetail/refundDetail?orderNo=' + item.orderNo
        }
        wx.navigateTo({
            url: url
        })
    },
    // 跳转详情
    toOrderDetails(e) {
        let {
            item
        } = e.currentTarget.dataset

        var url
        if (item.status == 0 || item.status == 1 || item.status == 6) {
            url = '/packageA/pages/Goods/book/bookDetail/bookDetail?orderNo=' + item.orderNo
            // }else if (item.status==2 || item.status==3 ){
            //   url =  '/packageA/pages/Goods/book/refund/refund?orderNo=' + item.orderNo
        } else if (item.status == 7) {
            url = '/packageA/pages/Goods/book/refundDetail/refundDetail?orderNo=' + item.orderNo
        }
        wx.navigateTo({
            url: url
        })
    },
    //跳转到电子券页面
    lookTacket() {
        wx.navigateTo({
            url: '../ticket/ticket',
        })
    },
    // 查看电子券
    lookElectronic(e) {
        console.log(e);
        this.orderno = e.currentTarget.dataset.orderno
        this.initData(e.currentTarget.dataset.orderno)


    },
    //点击已完成的删除
    deleteClosedOrder(e) { // 删除已经关闭订单
        const that = this
        common.showAlertView('提示', '是否删除该订单？', function (res) {
            if (res) {
                let {
                    item
                } = e.currentTarget.dataset
                let tempJson = {
                    userId: that.data.userInfo.user.id,
                    token: that.data.userInfo.token,
                    orderNo: item.orderNo
                }
                common.post('/venueOrder/deleteVenueOrder', tempJson, function (res) {
                    if (res.status == 200) {
                        common.showToast('删除成功')
                        that.reloadData()
                    } else {
                        common.showRequestErrMsg(res)
                    }
                })
            }
        })
    },
    //点击评价
    toEvaluate() {
        wx.showToast({
            title: '点击了评价',
        })
    },
    //返回上一页
    return() {
        wx.navigateBack({
            delta: 1,
        })
    },
    //点击返回上一页
    onClickLeft() {
        wx.navigateBack({
            delta: 1,
        })
    }
})